home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / UVirusCheck (PP) / UVirusCheck.sit / UVirusCheck / UVirusCheck.cp < prev    next >
Text File  |  1995-08-28  |  7KB  |  214 lines

  1. // ===========================================================================
  2. //    UVirusCheck.cp                    ⌐ 1995, âric Forget. All rights reserved.
  3. // ===========================================================================
  4. //    
  5. //    ************************************************************************
  6. //    *                                                                      *
  7. //    *    Before using this code you should read the "License Agreement"     *
  8. //    *    document and agree with it.                                        *
  9. //    *                                                                      *
  10. //    ************************************************************************
  11. //
  12. //    UVirusCheck implement a checksum (really basic one!) to the resource
  13. //    fork of an application. The first time you start your application it will
  14. //    make the checksum and write it directly in the resource fork. Each time
  15. //    later it will only read: so it is clean when sending it to customers.
  16. //
  17. // ---------------------------------------------------------------------------
  18. //
  19. //    Instruction Notes:
  20. //    -----------------
  21. //
  22. //    1) Include the file Virus.rsrc in your project;
  23. //
  24. //    2) In your resource editor, copy the content of '????' ID 128 resource;
  25. //
  26. //    3) Create a new type (your own) and paste the content copied in it.
  27. //       Be sure that your new resource as an ID equal to 128.
  28. //
  29. //    4) Create an Alert (with the ID you want). It must say something like:
  30. //
  31. //        The Application XXX is damaged or infected with a virus.
  32. //        Please install it again with originals disks.
  33. //
  34. //       Note: The default button should be "Quit"
  35. //
  36. //    5) At the beginning of your application (after initialized the toolbox!),
  37. //       add the following line:
  38. //
  39. //            StVirusCheck    MyVirusCheck(MY_RES_TYPE, MY_ALERT_ID);
  40. //
  41. // ---------------------------------------------------------------------------
  42. //
  43. //    Usage Notes:
  44. //    ------------
  45. //
  46. //    You may want to comment off the line at the instruction (5) when
  47. //    debugging with the Metrowerks debugger. If you don't, each time you
  48. //    enter the debugger, it will alert you the the application version is not
  49. //    the right one. It won't tell you that for the first execution after
  50. //    a build.
  51. //
  52. // ---------------------------------------------------------------------------
  53.  
  54. #include    "UVirusCheck.h"
  55. #include    "UEventUtils.h"
  56.  
  57.  
  58. // ---------------------------------------------------------------------------
  59. //        Ñ StVirusCheck
  60. // ---------------------------------------------------------------------------
  61.  
  62. StVirusCheck::StVirusCheck(
  63.     OSType        inResType,
  64.     ResIDT        inAlertID)
  65. {
  66.     mResourceMap = ::Get1Resource(inResType, 128);
  67.     mResType = inResType;
  68.     
  69.     Assert_(mResourceMap != nil);
  70.     ::SetResLoad(false);
  71.     
  72.     if((mResourceMap != nil) &&
  73.        (::GetHandleSize(mResourceMap) == sizeof(SResourceMapT)) &&
  74.        ((**(SResourceMapT **)mResourceMap).type == 'NBRM')) {
  75.        
  76.        BuildResourceMap();
  77.        
  78.     } else if((mResourceMap == nil) || (!CheckIfResourceMapIsInfected())) {
  79.     
  80.         ::Alert(inAlertID, nil);
  81.         ::ExitToShell();
  82.     }
  83.     
  84.     ::SetResLoad(true);
  85. }
  86.  
  87.  
  88. // ---------------------------------------------------------------------------
  89. //        Ñ ~StVirusCheck
  90. // ---------------------------------------------------------------------------
  91.  
  92. StVirusCheck::~StVirusCheck()
  93. {
  94.     if(mResourceMap != nil) {
  95.     
  96.         ::ReleaseResource(mResourceMap);
  97.     }
  98. }
  99.  
  100.  
  101. // ---------------------------------------------------------------------------
  102. //        Ñ BuildResourceMap
  103. // ---------------------------------------------------------------------------
  104.  
  105. void
  106. StVirusCheck::BuildResourceMap()
  107. {
  108.     LHandleStream        resourceMapStream(mResourceMap);
  109.     Int16                resourceTypeCount = ::Count1Types() - 9;
  110.     SResourceMapT        resourceMap;
  111.     Handle                aResource;
  112.     
  113.     
  114.     resourceMapStream.SetLength(sizeof(SResourceMapT) * resourceTypeCount);
  115.     resourceMapStream.SetMarker(0, streamFrom_Start);
  116.     
  117.     for(Int16 i = 1; i <= resourceTypeCount; i++) {
  118.     
  119.         ::Get1IndType(&resourceMap.type, i);
  120.         
  121.         if((resourceMap.type != mResType) &&
  122.            (resourceMap.type != 'SIZE') &&
  123.            (resourceMap.type != 'icl4') &&
  124.            (resourceMap.type != 'icl8') &&
  125.            (resourceMap.type != 'ICN#') &&
  126.            (resourceMap.type != 'ics4') &&
  127.            (resourceMap.type != 'ics8') &&
  128.            (resourceMap.type != 'ics#') &&
  129.            (resourceMap.type != 'MENU')) {
  130.             
  131.             resourceMap.count = ::Count1Resources(resourceMap.type);
  132.             resourceMap.size = 0;
  133.             
  134.             for(Int16 j = 1; j <= resourceMap.count; j++) {
  135.             
  136.                 aResource = ::Get1IndResource(resourceMap.type, j);
  137.                 resourceMap.size += ::GetResourceSizeOnDisk(aResource);
  138.             }
  139.             resourceMapStream.WriteData(&resourceMap, sizeof(SResourceMapT));
  140.         }
  141.     }
  142.     
  143.     resourceMapStream.DetachDataHandle();
  144.     
  145.     ::ChangedResource(mResourceMap);
  146.     ::WriteResource(mResourceMap);
  147. }
  148.  
  149.  
  150. // ---------------------------------------------------------------------------
  151. //        Ñ CheckIfResourceMapIsInfected
  152. // ---------------------------------------------------------------------------
  153.  
  154. Boolean
  155. StVirusCheck::CheckIfResourceMapIsInfected()
  156. {
  157.     Boolean                outIsInfected = true;
  158.     LHandleStream        resourceMapStream(mResourceMap);
  159.     Int16                resourceTypeCount = ::Count1Types() - 9;
  160.     SResourceMapT        resourceMap, resourceMapActual;
  161.     Handle                aResource;
  162.     
  163.     
  164.     if((resourceMapStream.GetLength() / sizeof(SResourceMapT)) == resourceTypeCount) {
  165.     
  166.         resourceMapStream.SetMarker(0, streamFrom_Start);
  167.         
  168.         for(Int16 i = 1; i <= resourceTypeCount; i++) {
  169.         
  170.             ::Get1IndType(&resourceMapActual.type, i);
  171.             
  172.             if((resourceMapActual.type != mResType) &&
  173.                (resourceMapActual.type != 'SIZE') &&
  174.                (resourceMapActual.type != 'icl4') &&
  175.                (resourceMapActual.type != 'icl8') &&
  176.                (resourceMapActual.type != 'ICN#') &&
  177.                (resourceMapActual.type != 'ics4') &&
  178.                (resourceMapActual.type != 'ics8') &&
  179.                (resourceMapActual.type != 'ics#') &&
  180.                (resourceMapActual.type != 'MENU')) {
  181.             
  182.                 resourceMapActual.count = ::Count1Resources(resourceMapActual.type);
  183.                 resourceMapActual.size = 0;
  184.                 
  185.                 for(Int16 j = 1; j <= resourceMapActual.count; j++) {
  186.                 
  187.                     aResource = ::Get1IndResource(resourceMapActual.type, j);
  188.                     
  189.                     resourceMapActual.size += ::GetResourceSizeOnDisk(aResource);
  190.                 }
  191.                 resourceMapStream.ReadData(&resourceMap, sizeof(SResourceMapT));
  192.                 
  193.                 if((resourceMapActual.type != resourceMap.type) ||
  194.                    (resourceMapActual.count != resourceMap.count) ||
  195.                    (resourceMapActual.size != resourceMap.size)) {
  196.                 
  197.                     outIsInfected = false;
  198.                     break;
  199.                 }
  200.             }
  201.         }
  202.     
  203.     } else {
  204.     
  205.         outIsInfected = false;
  206.     }
  207.     
  208.     resourceMapStream.DetachDataHandle();
  209.     
  210.     
  211.     return outIsInfected;
  212. }
  213.  
  214.